﻿using Microsoft.Phone.Shell;
using Tfs.OnTheRoad.Model;
using Tfs.OnTheRoad.Pages;
using Tfs.OnTheRoad.ViewModel;

namespace Tfs_OnTheRoad
{
    /// <summary>
    /// Work Items Details Page
    /// </summary>
    public partial class WorkitemDetailsPage : BasePage
    {
        public WorkitemDetailsPage()
        {
            InitializeComponent();
        }

        protected override void Refresh()
        {
            if (AttachmentsGrid.DataContext != null)
                ((AttachmentListViewModel)AttachmentsGrid.DataContext).LoadData();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            var viewModel = PhoneApplicationService.Current.State["CurrentWorkItem"] as WorkItem;
            if (viewModel != null)
            {
                if (AttachmentsGrid.DataContext == null)
                {
                    AttachmentListViewModel attachmentsViewModel = new AttachmentListViewModel(viewModel);
                    AttachmentsGrid.DataContext = attachmentsViewModel;
                    attachmentsViewModel.LoadData();
                }
                this.DataContext = viewModel;
            }
            var currentProject = PhoneApplicationService.Current.State["CurrentProject"] as Project;
            if (currentProject != null)
                HeaderControlPage.NavigationPath = string.Format("{0} \\ Work Item = {1}", currentProject.Name, viewModel.Id);
        }
    }
}
